package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_stat_keywords;

import cn.com.api.service.ApiU_stat_keywordsService;

import cn.com.util.*;


public class U_stat_keywordsBaseController {

	private static Logger logger = Logger.getLogger(U_stat_keywordsBaseController.class);
	
	@Autowired
	private ApiU_stat_keywordsService apiU_stat_keywordsService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_stat_keywords-list")
	public ModelAndView u_stat_keywords_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_stat_keywords_list.jsp");
		
		List<ApiU_stat_keywords> u_stat_keywordss = apiU_stat_keywordsService.findU_stat_keywordss();
		mv.addObject("u_stat_keywordss", u_stat_keywordss);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_stat_keywords-add-pre")
	public ModelAndView u_stat_keywords_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_stat_keywords_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_stat_keywords u_stat_keywords = apiU_stat_keywordsService.findU_stat_keywordsById(Integer.valueOf(id));
			mv.addObject("u_stat_keywords",u_stat_keywords);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_stat_keywords-add")
	public void u_stat_keywords_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String stat_id = request.getParameter("stat_id");
		String source = request.getParameter("source");
		String ip = request.getParameter("ip");
		String type = request.getParameter("type");
		String pv_count = request.getParameter("pv_count");
		String ip_count = request.getParameter("ip_count");
		String visit_count = request.getParameter("visit_count");
		String keyword = request.getParameter("keyword");
		String create_time = request.getParameter("create_time");
		String user_id = request.getParameter("user_id");
		String page_url = request.getParameter("page_url");
		String entry_url = request.getParameter("entry_url");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		String update_time = request.getParameter("update_time");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(stat_id==null || (stat_id.trim()).equals("")){
			ApiU_stat_keywords u_stat_keywords = new ApiU_stat_keywords();
            /*
		    u_stat_keywords.setStat_id(stat_id);
		    u_stat_keywords.setSource(source);
		    u_stat_keywords.setIp(ip);
		    u_stat_keywords.setType(type);
		    u_stat_keywords.setPv_count(pv_count);
		    u_stat_keywords.setIp_count(ip_count);
		    u_stat_keywords.setVisit_count(visit_count);
		    u_stat_keywords.setKeyword(keyword);
		    u_stat_keywords.setCreate_time(create_time);
		    u_stat_keywords.setUser_id(user_id);
		    u_stat_keywords.setPage_url(page_url);
		    u_stat_keywords.setEntry_url(entry_url);
		    u_stat_keywords.setExt_1(ext_1);
		    u_stat_keywords.setExt_2(ext_2);
		    u_stat_keywords.setExt_3(ext_3);
		    u_stat_keywords.setExt_4(ext_4);
		    u_stat_keywords.setExt_5(ext_5);
		    u_stat_keywords.setUpdate_time(update_time);
	        */
			apiU_stat_keywordsService.insertU_stat_keywords(u_stat_keywords);
		}
		//edit
		else{
			ApiU_stat_keywords u_stat_keywords = apiU_stat_keywordsService.findU_stat_keywordsById(Integer.valueOf(stat_id));
	        /*
		    u_stat_keywords.setStat_id(stat_id);
		    u_stat_keywords.setSource(source);
		    u_stat_keywords.setIp(ip);
		    u_stat_keywords.setType(type);
		    u_stat_keywords.setPv_count(pv_count);
		    u_stat_keywords.setIp_count(ip_count);
		    u_stat_keywords.setVisit_count(visit_count);
		    u_stat_keywords.setKeyword(keyword);
		    u_stat_keywords.setCreate_time(create_time);
		    u_stat_keywords.setUser_id(user_id);
		    u_stat_keywords.setPage_url(page_url);
		    u_stat_keywords.setEntry_url(entry_url);
		    u_stat_keywords.setExt_1(ext_1);
		    u_stat_keywords.setExt_2(ext_2);
		    u_stat_keywords.setExt_3(ext_3);
		    u_stat_keywords.setExt_4(ext_4);
		    u_stat_keywords.setExt_5(ext_5);
		    u_stat_keywords.setUpdate_time(update_time);
			*/
			apiU_stat_keywordsService.updateU_stat_keywords(u_stat_keywords);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_stat_keywords-delete")
	public void u_stat_keywords_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_stat_keywordsService.deleteU_stat_keywords(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



